library(tidyverse)
library(readxl)
library(janitor)
path <- "Excel/800-899/852/852 Pivot.xlsx"
input <- read_excel(path, range = "A2:B6")
test <- read_excel(path, range = "D2:E7")
result = input %>%
separate_longer_delim(cols = everything(), delim = "-") %>%
mutate(Amount = as.numeric(Amount)) %>%
summarise(`Total Amount` = sum(Amount, na.rm = TRUE), .by = Items) %>%
adorn_totals("row", name = "Grand Total")
all.equal(result, test , check.attributes = FALSE)
# [1] TRUEExcel BI - Excel Challenge 852
excel-challenges
excel-formulas
🔰 Answer Expected Items Amount Total Amount A-B-C 80-90-10 A C-Q-A 30-40-50 B

Challenge Description
🔰 Answer Expected Items Amount Total Amount A-B-C 80-90-10 A C-Q-A 30-40-50 B
Solutions
- Logic: Read the workbook ranges needed for the challenge; Derive the required intermediate columns; Parse the packed text or string structure; Aggregate or rank the data at the required grouping level.
- Strengths: The reshaping step mirrors the workbook output closely instead of forcing extra post-processing.
- Areas for Improvement: The solution assumes the workbook layout and selected ranges remain stable, so any structural change in the sheet would require small adjustments.
- Gem: The last reshape turns a raw transformation into something that already looks like a report.
import pandas as pd
path = "Excel/800-899/852/852 Pivot.xlsx"
input = pd.read_excel(path, usecols="A:B", skiprows=1, nrows=4)
test = pd.read_excel(path, usecols="D:E", skiprows=1, nrows=6).rename(columns=lambda col: col.replace('.1', ''))
output = (input.assign(Items=input['Items'].str.split('-'),
Amount=input['Amount'].str.split('-'))
.explode(['Items', 'Amount'])
.reset_index(drop=True)
)
summary = (output
.assign(Amount=pd.to_numeric(output['Amount']))
.groupby('Items', as_index=False)
.agg({'Amount': 'sum'})
)
total_row = pd.DataFrame([{'Items': 'Grand Total', 'Amount': summary['Amount'].sum()}])
summary = pd.concat([summary, total_row], ignore_index=True)
summary = summary.rename(columns={'Amount': 'Total Amount'})
print(test.equals(summary)) # TrueThe Python version follows the same grouped logic and keeps the transformation explicit in a dataframe pipeline.
Difficulty Level
Medium
The individual steps are manageable, but the correct transformation pattern is not obvious from the raw data.